Page({
  /**
   * 页面的初始数据
   */
  data: {
    data: {},
    src: '',
    painting: {},
    shareImage: '',
    height: 0,
    diffHeight: 0,
    imgHeight: 0,
    from: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      data: JSON.parse(options.data)
    })
    if (
      options.from &&
      options.from == 'neighbor'
    ) {
      this.setData({
        height: 730,
        imgHeight: 600,
        diffHeight: 430,
        from: options.from
      })
    } else {
      this.setData({
        height: 350,
        imgHeight: 190,
        diffHeight: 0,
        from: options.from
      })
    }
  },

  eventDraw() {
    wx.showLoading({
      title: '绘制分享图片中',
      mask: true
    })
    this.setData({
      painting: {
        width: wx.getSystemInfoSync().windowWidth,
        height: this.data.height,
        clear: true,
        views: [
          {
            type: 'image',
            url: '../../img/blackBg.jpg',
            top: 0,
            left: 0,
            width: wx.getSystemInfoSync()
              .windowWidth,
            height: this.data.height
          },
          {
            type: 'image',
            url: this.data.data.img,
            top: 0,
            left: 0,
            width: wx.getSystemInfoSync()
              .windowWidth,
            height: this.data.imgHeight
          },
          {
            type: 'image',
            url: '../../img/qrcode.jpg',
            top: 200 + this.data.diffHeight,
            left:
              wx.getSystemInfoSync().windowWidth -
              92,
            width: 80,
            height: 80
          },
          {
            type: 'text',
            content: this.data.data.name,
            fontSize: 16,
            color: '#ffffff',
            textAlign: 'left',
            top: 210 + this.data.diffHeight,
            left: 12,
            bolder: true
          },
          {
            type: 'text',
            content: this.data.data.address,
            fontSize: 16,
            color: '#ffffff',
            textAlign: 'left',
            top: 235 + this.data.diffHeight,
            left: 12
          },
          {
            type: 'text',
            content: this.data.data.date,
            fontSize: 16,
            lineHeight: 16,
            color: '#ffffff',
            textAlign: 'left',
            top: 262 + this.data.diffHeight,
            left: 12,
            bolder: true
          }
        ]
      }
    })
  },

  eventSave() {
    wx.saveImageToPhotosAlbum({
      filePath: this.data.shareImage,
      success(res) {
        wx.showToast({
          title: '保存图片成功',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },

  eventGetImage(event) {
    wx.hideLoading()
    const { tempFilePath, errMsg } = event.detail
    if (errMsg === 'canvasdrawer:ok') {
      this.setData({
        shareImage: tempFilePath
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    setTimeout(() => {
      this.eventDraw()
    }, 1000)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})
